<?php

namespace Api\Controller;

use Think\Controller;

class GameController extends PublicController
{

    public $userAwardModel = null;
    public $uId = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->uId = intval($this->get("id"));
        if (empty($this->uId)) {
            $this->json_error("请先登录！");
            exit;
        }

        $this->userAwardModel = M("user_award");
    }

    public function index()
    {
        $game = M("award")->where("del=0")->field("id,title,point,desc,img,probability,type")->select();
        foreach ($game as $key => $value) {
            $game[$key]['img'] = __DATAURL__ . $value['img'];
        }
        $this->json_result($game);

    }

    /**
     * 用户的抽奖行为以及返回用户抽奖中的结果
     * @author tiedong
     * @date 2018-01-29
     *
     */
    public function lotteryDraw()
    {
        $jifen = M("user")->where("id=" . $this->uId)->getField("jifen");
        if ($jifen < 100) {
            $this->json_error("积分不够");
            exit;
        }
        // 每天最多可以抽奖五次
//        $time = strtotime(date('Y-m-d'));
//        $todayTimes = $this->userAwardModel->where('uid=' . $this->uId . ' and addtime > ' . $time)->count();
//        if ($todayTimes > 4) {
//            $this->json_error("请明天再来，每日最多可抽奖5次");
//            exit;
//        }

        //用户点击抽奖减积分
        M("user")->where("id=" . $this->uId)->setDec("jifen", 100);
        $game = M("award")->where("del=0")->field("id,title,point,desc,img,probability,type")->select();
        foreach ($game as $key => $val) {
            $arr[$val['id']] = $val['probability'];
        }
        $prize_id = $this->get_rand($arr); //根据概率获取奖项id
        foreach ($game as $k => $v) { //获取前端奖项位置
            if ($v['id'] == $prize_id) {
                $prize_site = $k;
                break;
            }
        }
        $res = $game[$prize_id - 1]; //中奖项
        $data['prize_name'] = $res['title'];
        $data['id'] = $res['id'];
        $data['desc'] = $res['desc'];
        $data['prize_site'] = $prize_site;//前端奖项从-1开始

        //记录用户中奖的信心存进表里
        $award = M("award")->where("id=" . $res['id'])->find();
        if ($award['type'] == 1) {
            $da = array();
            $da['a_id'] = $res['id'];
            $da['uid'] = $this->uId;
            $da['addtime'] = time();
            $da['del'] = 0;
            $da['type'] = 1;
            $da['status'] = 1;
            $this->userAwardModel->add($da);
            $this->addJifen($this->uId, $award['point']);
            //赠送积分
            M("user")->where("id=" . $this->uId)->setInc("jifen", $award['point']);
        } else {
            $da = array();
            $da['a_id'] = $res['id'];
            $da['uid'] = $this->uId;
            $da['addtime'] = time();
            $da['del'] = 0;
            $da['type'] = 0;
            $da['status'] = 2;
            $this->userAwardModel->add($da);
        }
        $this->json_result($data);
    }

    public function addJifen($uid, $jifen)
    {
        $time = time();
        $sql = " INSERT INTO lr_user_sign (`uid`,`type`,`jifen`,`addtime`) VALUES ($uid,2,$jifen,$time)";
        M("user_sign")->execute($sql);
    }

    public function get_rand($proArr)
    {
        $result = "";
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset($proArr);
        return $result;

    }

    /**
     * 获取今日用户抽奖列表
     * @author tiedong
     * @date 2018-01-30
     */
    public function awardList()
    {
        $today = date("Y-m-d", time());
        $list = $this->userAwardModel->where("FROM_UNIXTIME(addtime,'%Y-%m-%d')='$today'")
            ->field("id,a_id,uid,type,addtime")
            ->order("addtime DESC")
            ->select();
        $data = array();
        foreach ($list as $key => $value) {
            $award['name'] = M("user")->where('id=' . $value['uid'])->getField("name");
            $award['photo'] = M("user")->where('id=' . $value['uid'])->getField("photo");
            if (!strstr($award['photo'], "https://")) {
                $award['photo'] = __DATAURL__ . "UploadFiles/" . $award['photo'];
            }
            $award['title'] = M("award")->where("id=" . $value['a_id'])->getField("title");
            $award['addtime'] = date("Y-m-d", $value['addtime']);
            array_push($data, $award);
        }
        $this->json_result($data);

    }

    /**
     * 用户获取奖品的列表
     * @author tiedong
     * @date 2018-01-30
     */
    public function getUserAward()
    {
        $list = $this->userAwardModel->where("uid=" . $this->uId)->field("id,a_id,uid,addtime,type,status,postage")->order("addtime DESC")->select();
        foreach ($list as $key => $value) {
            $list[$key]['name'] = M("user")->where('id=' . $value['uid'])->getField("name");
            $awardInfo = M("award")->where("id=" . $value['a_id'])->find();
            $list[$key]['title'] = $awardInfo['title'];
            $list[$key]['point'] = $awardInfo['point'];
            $list[$key]['desc'] = $awardInfo['desc'];
            $list[$key]['addtime'] = date("Y-m-d", $value['addtime']);
            $list[$key]['postage'] = $value['postage'];
        }
        $this->json_result($list);

    }

    /**
     * 用户兑换奖品
     * @author tiedong
     * @date 2018-01-30
     */
    public function duiAward()
    {
        $id = !empty($_GET['id']) ? intval($_GET['id']) : 0;
        $aId = !empty($_GET['aId']) ? intval($_GET['aId']) : 0;
        if (empty($id) && empty($aId)) {
            $this->json_error("操作有误");
            exit;
        }
        //判断
        $type = $this->userAwardModel->where("id=" . $id)->getField("type");
        if ($type == 1) {
            $this->json_error("不符合兑换");
            exit;
        }
        //判断
        $status = $this->userAwardModel->where("id=" . $id)->getField("status");
        if ($status == 1) {
            $this->json_error("已兑换");
            exit;
        }
        $address = M("address")->where("id=" . $aId)->find();
        $data = array();
        $data['receiver'] = $address['name'];
        $data['tel'] = $address['tel'];
        $data['address_xq'] = $address['region_name'] . $address['address'];
        $data['status'] = 1;
        $result = $this->userAwardModel->where("uid={$this->uId} AND id={$id}")->save($data);
        if ($result) {
            $this->json_result("兑换成功!");
            exit;
        } else {
            $this->json_error("兑换失败！");
            exit;
        }
    }
}

?>